package com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest;

import android.content.Context;

import com.mlz.mlzmall.bean.UserBean;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.utils.vali.Tools;

import java.util.Set;

/**
 * @description  登录工具类
 * Created by 安帅 on 2016/5/26 9:56.
 * PackageName com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.
 * ProjectName mlzmall_android.
 */
public class LoginUtils {

//    public static String LOGIN_TAG_PLATFORM_QQ = "QQ";//服务器上传平台标志
//    public static String LOGIN_TAG_LGOINGNAME = "loginName";
//    public static String LOGIN_TAG_PASSWORD = "password";
//
//    public static String THRIDLOGIN_PLATFORM_WEIXIN = "WEIXIN";//平台标志

    public static String TAG_THRIDLOGIN_PLATFORM = "TAG_THRIDLOGIN_PLATFORM";
    public static String TAG_THRIDLOGIN_OPENID = "TAG_THRIDLOGIN_OPENID";
    public static String TAG_THRIDLOGIN_ACCOUNT = "TAG_LOGIN_ACCOUNT";//保存的号码的位置一致

    public static String TAG_LOGIN_ACCOUNT = "TAG_LOGIN_ACCOUNT";
    public static String TAG_LOGIN_PASSWORD = "TAG_LOGIN_PASSWORD";

    private static String key = "MIsICdwIBADANsBgkqhkiaG9wB0AQEFAASCAmEwggJdAgEAAoGBAKB8GunT65dDzvV4898";


    /*
    后期需要提供密码保存加密的功能
         */
    public static void saveAccountPassword(Context context, String account, String password) {

        try {
            String accountTosave = Des.encrypt(account, key);
            String passwordTosave = Des.encrypt(password, key);

            Tools.setString(context, TAG_LOGIN_ACCOUNT, accountTosave);
            Tools.setString(context, TAG_LOGIN_PASSWORD, passwordTosave);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static String getSaveAccount(Context context) {

        String saved = Tools.getString(context, TAG_LOGIN_ACCOUNT, "");
        //解密过程
        String rusult = "";
        try {
            rusult = Des.decrypt(saved, key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rusult;

    }

    public static String getSavePassword(Context context) {

        String saved = Tools.getString(context, TAG_LOGIN_PASSWORD, "");
        String rusult = "";
        try {
            rusult = Des.decrypt(saved, key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rusult;

    }

    /*
      手动退出登录要将密码清空
     */
    public static void clearPassword(Context context) {
        Tools.setString(context, TAG_LOGIN_PASSWORD, "");
    }

    public static void clearAccount(Context context) {
        Tools.setString(context, TAG_LOGIN_ACCOUNT, "");
    }

    //没有测试QQ登录能否接收短信
    public static void loginSuccess(String account) {
        UserBean user = new UserBean();//登录成功，实例化User
        user.setAccout(account);
        MyApplication.getmInstance().setLoginUser(user);
    }

    public static boolean isLogin() {
        return MyApplication.getmInstance().isLogin();
    }

    public static void logout() {
        MyApplication.getmInstance().logOut();
        LoginUtils.clearPassword(MyApplication.getContext());
        LoginUtils.clearPlatformAndOpenId(MyApplication.getContext());

    }


    public static void saveThridLogin(Context context, String platform, String openId, String account) {
        try {
            String platformTosave = Des.encrypt(platform, key);
            String openIdTosave = Des.encrypt(openId, key);
            String accountTosave = Des.encrypt(account, key);

            Tools.setString(context, TAG_THRIDLOGIN_PLATFORM, platformTosave);
            Tools.setString(context, TAG_THRIDLOGIN_OPENID, openIdTosave);
            Tools.setString(context, TAG_THRIDLOGIN_ACCOUNT, accountTosave);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getSavePlatform(Context context) {
        String saved = Tools.getString(context, TAG_THRIDLOGIN_PLATFORM, "");
        String rusult = "";
        try {
            rusult = Des.decrypt(saved, key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rusult;
    }

    public static String getSaveOpenId(Context context) {
        String saved = Tools.getString(context, TAG_THRIDLOGIN_OPENID, "");
        String rusult = "";
        try {
            rusult = Des.decrypt(saved, key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rusult;
    }

    public static void clearPlatformAndOpenId(Context context) {
        Tools.setString(context, TAG_THRIDLOGIN_PLATFORM, "");
        Tools.setString(context, TAG_THRIDLOGIN_OPENID, "");
    }


}


